<?
    class Principio_Model_CompanyTypeExtTemplateMapper extends Principio_Library_Mapper
    {
        private static $db_table = null;


        private static function getDBTable()
        {
            if (self::$db_table == null)
                self::$db_table = new Principio_Model_DbTable_CompanyTypeExtTemplate();

            return self::$db_table;
        }


        /*
         * Delete by given SPA type
         */
        public static function deleteAllByType($type_id)
        {
            self::getDBTable()->delete(array("type_id=?" => $type_id));
        }


        public static function deleteAll($company_id)
        {
            self::getDBTable()->delete(array("company_id=?" => $company_id));
        }


        public static function delete($type_id, $company_id, $use_transaction = false)
        {
            try
            {
                if ($use_transaction)
                    self::getDBTable()->getAdapter()->beginTransaction();

                self::getDBTable()->delete(array("type_id=?" => $type_id, "company_id=?" => $company_id));

                if ($use_transaction)
                    self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                $return = self::reportException($ex);

                if ($use_transaction)
                {
                    self::getDBTable()->getAdapter()->rollback();
                    return false;
                }
                else
                    return $return;

            };
        }


        public static function update(Principio_Model_CompanyTypeExtTemplate $model_company_type_ext_template)
        {
            try
            {
                $type_id = $model_company_type_ext_template->getTypeID();
                $company_id = $model_company_type_ext_template->getCompanyID();

                self::getDBTable()->getAdapter()->beginTransaction();

                for ($i = 0; $i < $model_company_type_ext_template->getExts(); $i++)
                    self::getDBTable()->update(array("template" => $model_company_type_ext_template->getTemplate($i)), array("type_id=?" => $type_id, "ext_number=?" => $i, "company_id=?" => $company_id));

                self::getDBTable()->getAdapter()->commit();

            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                self::getDBTable()->getAdapter()->rollBack();

                return false;
            };
        }


        public static function insert(Principio_Model_Spa $model_spa)
        {
            for ($i = 0; $i <= $model_spa->getModelType()->getExts(); $i++)
                self::getDBTable()->insert(array("company_id" => $model_spa->getCompanyID(), "type_id" => $model_spa->getModelType()->getID(), "ext_number" => $i, "template" => ""));
        }


        public static function get($company_id, $type_id)
        {
            try
            {
                $select = self::getDBTable()->select()->where("type_id=?", $type_id)->where("company_id=?", $company_id);
                $values = self::getDBTable()->fetchAll($select);

                return new Principio_Model_CompanyTypeExtTemplate($values->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }
    }
?>